(() => {
  const svg = {
    unfold: `<svg t="1670170262966" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1919" width="18" height="30"><path d="M876 840c19.882 0 36 16.118 36 36s-16.118 36-36 36H148c-19.882 0-36-16.118-36-36s16.118-36 36-36h728z m0-243c19.882 0 36 16.118 36 36s-16.118 36-36 36H440c-19.882 0-36-16.118-36-36s16.118-36 36-36h436zM320.752 363.41a8 8 0 0 1 8 8v281.18a8 8 0 0 1-12.603 6.544l-199.848-140.59a8 8 0 0 1 0-13.087l199.848-140.59a8 8 0 0 1 4.603-1.458zM876 355c19.882 0 36 16.118 36 36s-16.118 36-36 36H440c-19.882 0-36-16.118-36-36s16.118-36 36-36h436z m0-243c19.882 0 36 16.118 36 36s-16.118 36-36 36H148c-19.882 0-36-16.118-36-36s16.118-36 36-36h728z" p-id="1920" fill="#1967d2"></path></svg>`,
    fold: `<svg t="1670170455539" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3907" width="18" height="18"><path d="M148 840c-19.882 0-36 16.118-36 36s16.118 36 36 36h728c19.882 0 36-16.118 36-36s-16.118-36-36-36H148z m0-243c-19.882 0-36 16.118-36 36s16.118 36 36 36h436c19.882 0 36-16.118 36-36s-16.118-36-36-36H148z m555.248-233.59a8 8 0 0 0-8 8v281.18a8 8 0 0 0 12.603 6.544l199.848-140.59a8 8 0 0 0 0-13.087l-199.848-140.59a8 8 0 0 0-4.603-1.458zM148 355c-19.882 0-36 16.118-36 36s16.118 36 36 36h436c19.882 0 36-16.118 36-36s-16.118-36-36-36H148z m0-243c-19.882 0-36 16.118-36 36s16.118 36 36 36h728c19.882 0 36-16.118 36-36s-16.118-36-36-36H148z" p-id="3908" fill="#1967d2"></path></svg>`
  };
  function findHeaders() {
    const map = {
      'club.tita.com': '#wrap',
      'mp.weixin.qq.com': '.rich_media_content',
      'juejin.cn': '.article-content',
      'www.oschina.net': '.article-box__inner',
      'cloud.tencent.com': '.cdc-layout__main',
      'juejin.cn': '.article-content',
    };
    const article = 
      document.querySelector('article') || 
      document.getElementById('page-content') || 
      document.querySelector(map[window.location.hostname]) || 
      document.body;
    if (!article) { return []; }
    return Array.prototype.slice.call(article.querySelectorAll('h1, h2, h3, h4, h5, h6'));
  }
  function createContentWrap() {
    if (document.querySelector('.shyTOC_wrap')) {
      document.querySelector('.shyTOC_wrap').remove();
    }
    const contentWrap = document.createElement('aside');
    contentWrap.className = 'shyTOC_wrap';
    const toc = document.createElement('div');
    toc.className = 'shyTOC_title';
    toc.innerText = '文章目录';
    contentWrap.append(toc);
    const fold = document.createElement('div');
    fold.innerHTML = svg.unfold;
    fold.className = 'shyTOC_fold';
    fold.onclick = function() {
      if (contentWrap.classList.contains('shyTOC_wrap--fold')) {
        contentWrap.classList.remove('shyTOC_wrap--fold');
        fold.innerHTML = svg.fold;
      } else {
        contentWrap.classList.add('shyTOC_wrap--fold');
        fold.innerHTML = svg.unfold;
      }
    };
    contentWrap.append(fold);
    const nav = document.createElement('nav');
    nav.className = 'shyTOC_nav';
    contentWrap.append(nav);
    document.body.append(contentWrap);
    return nav;
  }
  function createContentFragment(headers) {
    const fragment = document.createDocumentFragment();
    headers.forEach(el => {
      if (el.offsetWidth <= 0 && el.offsetHeight <= 0) { return; }
      const header = document.createElement('div');
      header.className = `shyTOC_item shyTOC_item--${el.tagName}`;
      header.innerText = el.innerText.replace(/\n/g, '');
      header.onclick = function() {
        el.scrollIntoView({behavior: "smooth", block: "start"});
      };
      fragment.append(header);
    });
    return fragment;
  }
  function init() {
    const headers = findHeaders();
    if (!headers.length) {
      console.log('[SHY_TOC] o(╥﹏╥)o 找不到标题，查看一下文章格式吧~');
      return;
    }
    const elWrap = createContentWrap();
    const fragment = createContentFragment(headers);
    elWrap.append(fragment);
  }
  init();
})();